/*
 * CadastrodeUsuario.java
 *
 * Created on 17 de Dezembro de 2008, 00:55
 */

package br.faimg.pomar.visual;

import br.faimg.pomar.controle.componentevisual.TableGrupodeUsuario;
import javax.swing.JTable;
import javax.swing.table.TableModel;

/**
 *
 * @author  diogo
 */
public class CadastrodeUsuario extends javax.swing.JInternalFrame {

    /** Creates new form CadastrodeUsuario */
    public CadastrodeUsuario() {
        initComponents();
        
        alterarjButton2.setEnabled(false);
        
        updateTable();
    }
    
    
     private void updateTable() {
        //Lista os itens cadastrados no banco na tablela
        tabelaCadastrodeUsuario.setModel(new JTable(
                new TableGrupodeUsuario().getTableModel()).getModel());
        
        //remove a coluna do codigo do produto
        tabelaCadastrodeUsuario.removeColumn(tabelaCadastrodeUsuario.getColumn("GrupoCod"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelaCadastrodeUsuario = new javax.swing.JTable();
        incluirjButton1 = new javax.swing.JButton();
        alterarjButton2 = new javax.swing.JButton();
        cancelarjButton3 = new javax.swing.JButton();

        setTitle("Cadastro de Usuários");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tabelaCadastrodeUsuario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabelaCadastrodeUsuario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaCadastrodeUsuarioMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tabelaCadastrodeUsuario);

        jPanel1.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 570, 320));

        incluirjButton1.setText("Incluir");
        incluirjButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                incluirjButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(incluirjButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 350, 90, -1));

        alterarjButton2.setText("Alterar");
        alterarjButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarjButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(alterarjButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 350, 80, -1));

        cancelarjButton3.setText("Cancelar");
        cancelarjButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarjButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(cancelarjButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 350, 90, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 590, 380));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void tabelaCadastrodeUsuarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaCadastrodeUsuarioMouseClicked

    if (tabelaCadastrodeUsuario.getSelectedRow() >= 0){
            alterarjButton2.setEnabled(true);
            
    } else {
        alterarjButton2.setEnabled(false);
    }

}//GEN-LAST:event_tabelaCadastrodeUsuarioMouseClicked

private void incluirjButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_incluirjButton1ActionPerformed
   CadastrodeUsuarioInclusao view = new CadastrodeUsuarioInclusao();
    view.setModal(true);
    view.setResizable(false);
    view.setVisible(true);
    
    updateTable();
}//GEN-LAST:event_incluirjButton1ActionPerformed

private void alterarjButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarjButton2ActionPerformed
   TableModel tableTemp = tabelaCadastrodeUsuario.getModel();
   int linha = tabelaCadastrodeUsuario.getSelectedRow();
   
    CadastrodeUsuarioInclusao view = new CadastrodeUsuarioInclusao((String)tableTemp.getValueAt(linha, 0), 
                                                                             (String)tableTemp.getValueAt(linha, 2),
                                                                             (String)tableTemp.getValueAt(linha, 1),
                                                                             (String)tableTemp.getValueAt(linha, 4));
                                                                             
    view.setModal(true);
    view.setResizable(false);
    view.setVisible(true);
    
    updateTable();
}//GEN-LAST:event_alterarjButton2ActionPerformed

private void cancelarjButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarjButton3ActionPerformed
    this.setVisible(isIcon);
}//GEN-LAST:event_cancelarjButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarjButton2;
    private javax.swing.JButton cancelarjButton3;
    private javax.swing.JButton incluirjButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tabelaCadastrodeUsuario;
    // End of variables declaration//GEN-END:variables

}
